package com.ericsson.common.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYYMMDD_HH = "yyyyMMdd_HH";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String getMyTime(){return dateTimeNow(YYYYMMDD_HH);}

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     *获取间隔日期
     * @param past 时间间隔
     * @param dateFormat 返回的日期格式
     * @return
     */
    public static  String getPastDate(int past,String dateFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        String result = format.format(today);
        return result;
    }

    /**
     * 返回间隔分钟的时间
     * @param minute
     * @return
     */
    public static String getMinutePlus(int minute,String formatter){
        LocalDateTime time = LocalDateTime.now().plusMinutes(minute);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatter);
        String createTime = dateTimeFormatter.format(time);
        return createTime;
    }

    /**
     * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String getDateTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate = null;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * 根据已知的参数时间获取后n分钟的时间
     * @param fromDateTime
     * @param nMin
     * @return
     */
    public static String getAfterNMinDateTime(String fromDateTime,int nMin,String format) {
        DateFormat df = new SimpleDateFormat(format);
        String newDateTime = "";
        try {
            Date currentDate = df.parse(fromDateTime);
            Date date = new Date();
            date.setTime(currentDate.getTime()+nMin*60*1000);
            newDateTime = df.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDateTime;
    }

    /**
     * 根据当前日期获取前n天日期
     * @param date
     * @param day
     * @return
     */
    public static String beforNumDay(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, day);
        return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    }

    /**
     * 根据当前日期获取前n天日期
     * @param date
     * @param day
     * @return
     */
    public static String beforNumDay(Date date, int day,String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, day);
        return new SimpleDateFormat(format).format(c.getTime());
    }


    /**
     * 当前时间向推几小时
     * @param ihour 小时
     * @return String
     */
    public static String dateRoll(int ihour) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYYMMdd_HH");
        //（1）获取当前时间
        LocalDateTime date = LocalDateTime.now();
        //（2）获取当前时间的前几小时时间
        LocalDateTime localDateTime = date.minusHours(ihour);

        return dateTimeFormatter.format(localDateTime);
    }


    /**
     * 当前时间向推几小时
     * @param ihour 小时
     * @return String
     */
    public static String dateRollByFormat(int ihour,String format) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        //（1）获取当前时间
        LocalDateTime date = LocalDateTime.now();
        //（2）获取当前时间的前几小时时间
        LocalDateTime localDateTime = date.minusHours(ihour);

        return dateTimeFormatter.format(localDateTime);
    }

    /**
     * 根据已知的参数时间获取前n分钟的时间
     * @param fromDateTime
     * @param nMin
     * @return
     */
    public static String getBeforeNMinDateTime(String fromDateTime,int nMin,String format) {
        DateFormat df = new SimpleDateFormat(format);
        String newDateTime = "";
        try {
            Date currentDate = df.parse(fromDateTime);
            Date date = new Date();
            date.setTime(currentDate.getTime()-nMin*60*1000);
            newDateTime = df.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDateTime;
    }



    public static String getStatisTime(int hourNumber,Date dateFile,String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateFile);//date 换成已经已知的Date对象
        cal.add(Calendar.MINUTE, -hourNumber);// before hour
        DateFormat dateFormat = new SimpleDateFormat(format);
        String dateString = dateFormat.format(cal.getTime());
        return dateString;
    }

    public static void main(String[] args) throws ParseException, IOException {
        System.out.println(parseDate("2021-11-19"));
        System.out.println(dateTime(YYYY_MM_DD,"2021-11-19"));
    }

    public static String getEbmLogStartMinTime(int minNumber) {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, -minNumber);
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd.HHmm");
        String dateString = dateFormat.format(cal.getTime());
        String flag = dateString.substring(dateString.length()-2, dateString.length());
        if(Integer.parseInt(flag)<15&&Integer.parseInt(flag)>=0){
            return "A"+dateString.substring(0, dateString.length()-2)+"00";
        }else if(Integer.parseInt(flag)<30&&Integer.parseInt(flag)>=15){
            return "A"+dateString.substring(0, dateString.length()-2)+"15";
        }else if(Integer.parseInt(flag)<45&&Integer.parseInt(flag)>=30){
            return "A"+dateString.substring(0, dateString.length()-2)+"30";
        }else{
            return "A"+dateString.substring(0, dateString.length()-2)+"45";
        }
    }

    public static String getEbmLogStartMinDateTime(int minNumber) {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, -minNumber);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateString = dateFormat.format(cal.getTime());
        String flag = dateString.substring(dateString.length()-2, dateString.length());
        if(Integer.parseInt(flag)<15&&Integer.parseInt(flag)>=0){
            return dateString.substring(0, dateString.length()-2)+"00:00";
        }else if(Integer.parseInt(flag)<30&&Integer.parseInt(flag)>=15){
            return dateString.substring(0, dateString.length()-2)+"15:00";
        }else if(Integer.parseInt(flag)<45&&Integer.parseInt(flag)>=30){
            return dateString.substring(0, dateString.length()-2)+"30:00";
        }else{
            return dateString.substring(0, dateString.length()-2)+"45:00";
        }
    }

    public static String getProcLogStartMinDateTime(int minNumber) {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, -minNumber);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm");
        String dateString = dateFormat.format(cal.getTime());
        String flag = dateString.substring(dateString.length()-2);
        if(Integer.parseInt(flag)<5&&Integer.parseInt(flag)>=0){
            return dateString.substring(0, dateString.length()-2)+"00";
        }else if(Integer.parseInt(flag)<10&&Integer.parseInt(flag)>=5){
            return dateString.substring(0, dateString.length()-2)+"05";
        }else if(Integer.parseInt(flag)<15&&Integer.parseInt(flag)>=10){
            return dateString.substring(0, dateString.length()-2)+"10";
        }else if(Integer.parseInt(flag)<20&&Integer.parseInt(flag)>=15){
            return dateString.substring(0, dateString.length()-2)+"15";
        }else if(Integer.parseInt(flag)<25&&Integer.parseInt(flag)>=20){
            return dateString.substring(0, dateString.length()-2)+"20";
        }else if(Integer.parseInt(flag)<30&&Integer.parseInt(flag)>=25){
            return dateString.substring(0, dateString.length()-2)+"25";
        }else if(Integer.parseInt(flag)<35&&Integer.parseInt(flag)>=30){
            return dateString.substring(0, dateString.length()-2)+"30";
        }else if(Integer.parseInt(flag)<40&&Integer.parseInt(flag)>=35){
            return dateString.substring(0, dateString.length()-2)+"35";
        }else if(Integer.parseInt(flag)<45&&Integer.parseInt(flag)>=40){
            return dateString.substring(0, dateString.length()-2)+"40";
        }else if(Integer.parseInt(flag)<50&&Integer.parseInt(flag)>=45){
            return dateString.substring(0, dateString.length()-2)+"45";
        }else if(Integer.parseInt(flag)<55&&Integer.parseInt(flag)>=50){
            return dateString.substring(0, dateString.length()-2)+"50";
        }else{
            return dateString.substring(0, dateString.length() - 2) + "55";
        }

    }

    public static String formattedDateTime(String input, String inputFormat, String outputFormat) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputFormat);
        LocalDateTime localDateTime = LocalDateTime.parse(input, inputFormatter);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormat);
        return localDateTime.format(outputFormatter);
    }
}
